<template>
  <el-dialog
      :title="title"
      custom-class="x-large-dialog"
      :visible="visible"
      append-to-body
      @open="mixinOpen"
      @close="close"
  >
    <div class="search-pannel">
      <el-form
          class="search-form"
          :model="postData"
          label-position="top"
          :rules="postRules"
          ref="postForm"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="角色名字" prop="name">
              <el-input v-model="postData.name" placeholder="请输入角色名字"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="权限配置" prop="auth">
              <el-tree
                  ref="tree"
                  :data="menuListCom"
                  :props="defaultProps"
                  show-checkbox
                  node-key="id"
              >
              </el-tree>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="角色描述" prop="description">
              <el-input type="textarea" :rows="6" v-model="postData.description" placeholder="请输入角色描述"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div slot="footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { _post, _put, _getDetail } from '../src/store'
import { getAllMenu, postDialog } from '@/mixins'

export default {
  name: 'postDialog',
  mixins: [postDialog, getAllMenu],
  data() {
    return {
      postData: {
        name: '',
        auth: '',
        description: ''
      },
      postRules: {
        name: [{ required: true, message: '请输入角色名字', trigger: 'blur' }],
        auth: [{ required: true, message: '请输入选择权限', trigger: 'submit' }],
        description: [{ required: true, message: '请输入角色描述', trigger: 'blur' }]
      },
      addFunc: _post,
      editFunc: _put,
      detailFunc: _getDetail
    }
  },
  methods: {
    save() {
      const tempArr = this.$refs.tree.getCheckedNodes(false, true)
      this.postData.auth = tempArr.map(item => item.id).sort((a, b) => { return a - b }).join()
      this.submit()
    }
  },
  computed: {
    menuListCom() {
      return this.menuList.map(item => {
        if (item.type === 'PC') {
          item.name = item.name + '(PC端)'
        } else {
          item.name = item.name + '(移动端)'
        }
        return item
      })
    }
  }
}
</script>

<style scoped>

</style>
